var ajaxResponseObj;
var CAT = 0;
var DIAGRAMS = 0; //select Diagram option
var CategoriesCache = {}; //object
var TotalsCache = {};
var StatsCache = {};
var shortMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var COLORS = ['#4572A7', '#C33E31', '#86A24C', '#3D2614'];
var PlotLabels=[];

/**
 * Am object that contain info about checkboxes that must be hidden.
 */
var CustomGraphsHidden = {};


/**
 * Request parameters and their values obtained from form fields.
 */
var REQUEST_DATA = {  
action: "get-totals"
}


function GetStats()
{
    $.ajax({
      type: "GET",
      url: "../../../admin/api/GetOverviewData.php",
      data: REQUEST_DATA,
      success: getTotalsStatus
    });
}

function getTotalsStatus(obj)
{

   if (typeof TotalsCache['totals'] == 'undefined')
   {
    ajaxResponseObj = eval('(' + obj + ')');
    TotalsCache['totals'] = ajaxResponseObj;
    GetTotals(ajaxResponseObj);  
   }
   
   else GetTotals(TotalsCache['totals']);
  
  REQUEST_DATA.action = "get-markets";

   $.ajax({
      type: "GET",
      url: "../../../admin/api/GetOverviewData.php",
      data: REQUEST_DATA,
      success: getMarketsStatus
    });
}

function getMarketsStatus(obj)
{
    ajaxResponseObj = eval('(' + obj + ')');
    TotalsCache['markets'] = ajaxResponseObj;
    GetMarkets(ajaxResponseObj);  
}


function GetTotals(obj)
{
  var Suma = 0;
  var Totals;
  var Count = 0;
  var i=1;
 
  var html = '<table border="0" width="100%" cellspacing="0" cellpadding="0" class="scrollTable">'
  + '<thead class="fixedHeader"><tr class="alternateRow">'
  + '<th class="green">ID</th>'
  + '<th class="green">Category_name</th>'
  + '<th class="green">Products</th>'
  + '<th class="green">Offers</th>'
  + '</tr></thead>'
  + '<tbody class="scrollContent">';
 
  for (var key in obj['data'])
    {
     
      html += '<tr>'
//        <td>'+ obj['data'][key]['category_id'] +'</td>'
      +'<td>'+ i++  +'</td>'
      +'<td>'+ obj['data'][key]['name'] +'</td>'
      +'<td>'+ obj['data'][key]['Produse'] +'</td>'
      +'<td>'+ obj['data'][key]['Oferte'] +'</td>'
      +'</tr>';
    
      Suma += Math.round(obj['data'][key]['Produse']);
      Count++;
    }
    
  html+= '</tbody></table>';
  $('#tableContainer').html(html);
  
  Totals = '<ul><li>Categories: ' + obj['categorii'][0]['Categorii'] + '</li><li>Items: '  + obj['categorii'][0]['produse'] + '</li>';
  Totals += '<li>Markets: ' + obj['magazine'][0]['Magazine'] + '</li><li>Offers: '  + obj['magazine'][0]['Oferte'] + '</li></ul>'; 
  $('#totals').html(Totals);
  
}

function CheckResponseStatus()
{
    GetStats();
}

function GetMarkets(obj)
{
 var i=1;
  var html = '<table border="0" width="100%" cellspacing="0" cellpadding="0" class="scrollTable2">'
  + '<thead class="fixedHeader2"><tr class="alternateRow">'
  + '<th class="green">ID</th>'
  + '<th class="green">name</th>'
  + '<th class="green">Items</th>'
  + '</tr></thead>'
  + '<tbody class="scrollContent2">';
 
  for (var key in obj['data'])
    {
     
      html += '<tr>'
      +'<td>'+ i++  +'</td>'
      +'<td>'+ obj['data'][key]['name'] +'</td>'
      +'<td>'+ obj['data'][key]['Items'] +'</td>'
      +'</tr>';
    }
    
  html+= '</tbody></table>';
  $('#tableContainer2').html(html);

}

